/*
 * Copyright (C) 2007-2008 SlideMe client
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.android.manager;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class SettingsConfigActivity extends Activity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        this.setContentView(R.layout.settings_layout);

        SamApplication samApplication = (SamApplication) getApplication();
        final ApplicationManagerContext ctx = samApplication.getApplicationManagerContext();

        final TextView updateText = (TextView) this.findViewById(R.id.settings_server_url_edit);
        updateText.setText(ctx.getCatalogServerURI());

        ImageButton update = (ImageButton) findViewById(R.id.settings_server_update_button);

        update.setOnClickListener(new ImageButton.OnClickListener() {
            public void onClick(View view) {
                if (updateText.getText() != null && updateText.getText().equals(ctx.getCatalogServerURI())) {
                    return;
                }

                FileOutputStream fos = null;
                try {
                    Properties prop = new Properties();
                    prop.setProperty("catalogURI", updateText.getText().toString());
                    fos = SettingsConfigActivity.this.openFileOutput("server.conf", Context.MODE_PRIVATE);
                    prop.store(fos, "");
                    ctx.setCatalogServerURI(updateText.getText().toString());

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (fos != null) fos.close();
                    } catch (IOException e) {
                    }
                }
            }
        });


        ImageButton myAppsView = (ImageButton) this.findViewById(R.id.settings_layout_myapps_button);
        ImageButton settingsView = (ImageButton) this.findViewById(R.id.settings_layout_settings_button);
        ImageButton applicationsView = (ImageButton) this.findViewById(R.id.settings_layout_applications_button);
        ViewContext.setMenuViewListeners(myAppsView, settingsView, applicationsView, this);
    }
}
